package cn.action;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.domain.Api;
import cn.domain.Proj;
import cn.domain.User;
import cn.util.Pagination;
import cn.util.Util;

/**
 * 
 * @项目名称:pigbaby
 * @类名称:ApkAction
 * @类描述:移动设备的所有接口
 * @创建人:陈凯
 * @创建时间:20150713 17：23 星期一
 * @修改人:
 * @修改时间:
 * 
 * @修改备注:
 * @version
 */
@Namespace("/")
@Scope("prototype")
@ParentPackage("json-default")
@Component("projAction")
@Action(value = "projAction", results = { 
		@Result(name = "loginout", location = "/login.jsp"),
		@Result(name = "apilist", location = "/apireport.jsp"),
		@Result(name = "index", location = "/index.jsp"),
		@Result(name = "resultjson", type = "json")
		})
public class ProjAction extends BaseAction {

	// 保存日志
	public static Logger logger = LoggerFactory.getLogger(ProjAction.class);
	private String deleteId;
	private Proj proj;
	private String aid;
	private String status;
	private String startDate;
	private String endDate;
	private String aname;
	private String dname;
	public String gotolist(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		return "index";
	}
	
	public String destroy(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		if(deleteId != null && !"".equals(deleteId)){
			projService.delete(deleteId);
		}
		outJSONMessage("success");
		return "resultjson";
	}

	public String insert(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		if(projService.getOne(proj)){
			outJSONMessage("该项目已经存在！");
		}else{
			proj.setAddtime(Util.DateToInt(new Date()));
			proj.setIsdel(0);
			projService.insert(proj);
			outJSONMessage("success");
		}
		return "resultjson";
	}
	
	public String list() throws UnsupportedEncodingException{
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		String pagestr = request.getParameter("page");
		pageIndex = Integer.parseInt(pagestr);
		String search = "";
		if(proj != null){
			search = new String(proj.getCname().getBytes("iso8859-1"),"UTF-8");
		}
		total = projService.getAllCount(proj);
		pagination = new Pagination(pageIndex,rows,total);
		pagination.setSearch(search);
		List<Proj> list = projService.getAlllist(pagination);
		outJSONList(list,total);
		return "resultjson";
	}

	public String edit(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		if(deleteId != null && !"".equals(deleteId)){
			proj = projService.selectOne(deleteId);
		}
		outJSONObject(proj);
		return "resultjson";
	}
	
	public String update(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		Proj proj_db = projService.selectOne(proj.getAid()+"");
		if(!proj_db.getCname().equals(proj.getCname())){
			if(projService.getOne(proj)){
				outJSONMessage("该项目已经存在！");
			}else{
				proj_db.setCname(proj.getCname());
				proj_db.setCdesc(proj.getCdesc());
				projService.update(proj_db);
				outJSONMessage("success");
			}
		}else {
			proj_db.setCname(proj.getCname());
			proj_db.setCdesc(proj.getCdesc());
			projService.update(proj_db);
			outJSONMessage("success");
		}
		return "resultjson";
	}
	
	public String gotoApi(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		return "apilist";
	}
	
	public String apiList() throws UnsupportedEncodingException{
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		String pagestr = request.getParameter("page");
		pageIndex = Integer.parseInt(pagestr);
		pagination = new Pagination(pageIndex,rows,total);
		pagination.setAid(Integer.parseInt(aid));
		pagination.setStatus(status);
		pagination.setStartDate(startDate);
		pagination.setEndDate(endDate);
		if(null != aname && !"".equals(aname.trim())){			
			pagination.setaUserName(new String(aname.getBytes("iso8859-1"),"UTF-8"));
		}
		
		if(null != dname && !"".equals(dname.trim())){
			pagination.setdUserName(new String(dname.getBytes("iso8859-1"),"UTF-8"));
		}
		
		total = projService.getApiCount(pagination);
		List<Api> list = projService.getApiList(pagination);
		
		outJSONList(list,total);
		return "apilist";
	}
	
	public String getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public Proj getProj() {
		return proj;
	}

	public void setProj(Proj proj) {
		this.proj = proj;
	}

	
	public String getAid() {
		return aid;
	}

	public void setAid(String aid) {
		this.aid = aid;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getAname() {
		return aname;
	}

	public void setAname(String aname) {
		this.aname = aname;
	}

	public String getDname() {
		return dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}

	
	
}
